#!/usr/bin/python

# deps: dominate

import re
import os
import sys
import hashlib
import os.path
import subprocess
import dominate
from dominate import tags

multimedia_filenames = r".*\.(?:webm|avi|mp4|mkv|mp3|flac|ogg|opus|wav)$"

def process_dir(path, doc):
    
    for path, directories, files in os.walk(path):
        for f in files:
            if re.match(multimedia_filenames, f, re.IGNORECASE):
                # print('found %s' % os.path.join(path, f))

                identifier = hashlib.sha256(f.encode()).hexdigest()[0:16]
                outfilename = identifier + ".png"
                
                result = subprocess.run(['ffmpeg', '-i', os.path.join(path, f), '-filter_complex', 'showwavespic=s=640x120', '-frames:v', '1', outfilename], capture_output=True, text=True)
                
                doc += tags.div(f)
                doc += tags.img(src=outfilename )


if __name__=='__main__':
    thedir = sys.argv[1]
    doc = dominate.document(title='Audio file report for %s' % thedir)
    process_dir(path=thedir, doc=doc)
    print(doc)
